<template>
	<view class="">
		<search @gosearchPost="gosearchPost"></search>
		<card  :postsRecord="searchpost" :uid="uid" :cangodetail="cangodetail" :isshowcomment="isshowcomment"></card>
		<view class="none" v-if="searchpost && !searchpost.length">
			暂无搜索记录
		</view>
	</view>
</template>

<script>
	
	import api from '../../../common/api.js'
	import card from '../../../components/common/card.vue'
	import search from './search.vue'
	export default {
		data() {
			return {
				content:''		,//输入文本内容
				searchpost:[]	,//搜索打卡记录
				isshowcomment:1,//是否展示评论数据
				cangodetail:1,	//打卡是否能跳转到详情页
				uid:null,			  //当前登录用户id
				post:[],				//热门打卡数据
				me:[],					//用户信息
			}	
		},
		components: {
			search,
			card,
		},
		
		async onLoad(p) {
			this.content = p.content
			this.uid = p.uid
			await this.getMe()	
		},
		
		methods:{
			async gosearchPost(val) {
				if(val == '' || val == null) {	
					uni.showToast({
						title:'搜索内容不能为空',
						icon:'none'
					})
				}else {
					this.searchpost = await api.GetSearchPost(val)
				}
				
			},
			
			async getMe() {// 获取当前登录用户信息
				let res = await api.GetMe()
				this.$store.commit('setPhone',res[0].phone)
				this.had_phone = res[0].purePhoneNumber
				this.uid = res.map(item => item.id)
				this.me = res[0]
				this.flag = true //确保子组件能获取到数据
			},
		},
		
		
		
		async updated() {//更新热门打卡
			var _that = this
			uni.$off('updateHotpostComment')
			uni.$once('updateHotpostComment',(comment_item,index) => {
				console.log(_that.post[index].commented);
				console.log('更新下评论好不好');
				// _that.post =  await _that.fetch(_that.page,_that.limit)
				_that.post[index].commented += 1
				_that.post[index].hot_comments.unshift(comment_item)
			})
			uni.$off('updateHotPost')
			uni.$on('updateHotPost',async function(index,item) {//点赞，更新热门打卡
				if(item.userliked){
					console.log('userliked');
				  item.liked --
				  item.userliked = false
					item.likers.splice(item.likers.indexOf(item.likers.find(el => { return el.id == _that.me.id} )),1)
				  let res = await  api.UnLikePost(item.id)
				  _that.post[index] = item
					
				}else{
					console.log('userunliked');
				  item.liked ++
				  item.userliked = true
				  let res  = await  api.LikePost(item.id)
					item.likers.push(_that.me)
				  _that.post[index] = item
					
				}
			})
		},
	}
</script>

<style lang="scss">
.none {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100vh;
	padding: 50rpx;
	font-size: 32rpx;
	color: #A7AAB5;
}
</style>
